/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Akanksha;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.ArrayList;

/**
 *
 * @author Akanksha Chauhan
 */
public class InsulinSensor {
   
    private static double totalInsulinAmount;
    private static Timer insulinTimer ;
    public static boolean basalActivationTrigger;
    
    public void startInsulinBolus(double insulinamount )
    {
        if(checkInsulinStatus(insulinamount))
        {
            Timer timer = new Timer();
            //calculates the amount of insulin to be given per millisecond
            double insulinAmountPerMs = insulinamount / 60000;
            timer.start();
            while(timer.getElapsedTime()<= 60000)
            {
                totalInsulinAmount = totalInsulinAmount - insulinAmountPerMs;
            }
            timer.stop();
        }
       // else
            //alarm;
        
    } 
    public static void startInsulinBasalProfile( int[] basalProfileInsulinAmount)
    {
        boolean continueTrigger  = checkOtherActiveBasalProfile();
        if(continueTrigger)
        {
                basalActivationTrigger = true;
                int currentHour = Clock.GetCurrentHour();
               // counter keeps a check on currentHour .So that the amount of insulin could be set according to what the current time is.
                for( int counter = currentHour ; counter <= 23 ; counter++ )
                {
                    //this loop will check the passage time after every 1 minute
                    while(Clock.GetCurrentHour()== counter )
                    {
                        double insulinAmount = basalProfileInsulinAmount[counter-1];
                        if(checkInsulinStatus(insulinAmount))
                         {
                             //calculates the amount of insulin to be given per second
                              double insulinAmountPerSec = insulinAmount / 3600;

                              // this loop will run for 1 minute
                              Timer t = new Timer();
                              t.start();
                              for(int i =1;i<=60;i++)
                              {
                                while(t.getElapsedTimeSecs()<= i)
                                  {
                                      //wait
                                  }
                                totalInsulinAmount = totalInsulinAmount - insulinAmountPerSec;
                              }
                              // timer would be stopped after 1 minute
                              t.stop();

                         }
                     }
                }
            }
    }
    
    public void stopInsulin()
    {
         insulinTimer.stop();
    }
    public void pauseInsulin()
    {
       insulinTimer.pause();
    }
    public void resumeInsulin()
    {
        insulinTimer.resume();
    }
    public static boolean checkInsulinStatus(double insulinAmount)
    {
       if(totalInsulinAmount < insulinAmount ) 
       {
           return false;   
       }
       else
           return true;
    }
    public static boolean checkOtherActiveBasalProfile()
    {
        
         boolean continueTrigger;   
        //checks the basalActivationTrigger 
        if(basalActivationTrigger)
        {
            Alarm alarm = new Alarm();
            int result = alarm.confirm("Another basal profile is already active .Do you still want to continue");
            if(result == 1)
                continueTrigger = true;
            else
                continueTrigger = false;
        }
        else
           continueTrigger = true;
        
        return continueTrigger;
            
    }
}
